package com.cfushn.ddd.mp.mapper;

import com.cfushn.ddd.mp.crud.Crud;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * StaticMapper<p/>静态CRUD方法
 * 注意: 静态mapper默认无法使用@DS数据源切换
 *
 * @author CFuShn 2022/3/9 15:39
 */
@SuppressWarnings("unused")
public class StaticMapper implements ApplicationContextAware {

    private static Crud mapper;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        mapper = applicationContext.getBean(CommonMapper.class);
    }

    public static int insert(String sql) {
        return mapper.insert(sql);
    }

    public static int delete(String sql) {
        return mapper.delete(sql);
    }

    public static int update(String sql) {
        return mapper.update(sql);
    }

    /**
     * 查询,支持接收任意结果集
     *
     * @param sql sql
     * @return List<Map < String column, Object value>>
     */
    public static List<Map<String, Object>> select(String sql) {
        return mapper.select(sql);
    }

    /**
     * 查询单列,返回List<列值>
     *
     * @param sql   String
     * @param clazz Class
     * @return List<T>
     * @author CFuShn at 2022/2/18 18:38
     */
    public static <T> List<T> select(String sql, Class<T> clazz) {
        return mapper.select(sql, clazz);
    }

    /**
     * 查询单值或单个对象
     *
     * @param sql   String
     * @param clazz Class
     * @return T
     * @author CFuShn at 2022/1/19 16:11
     */
    public static <T> T selectOne(String sql, Class<T> clazz) {
        return mapper.selectOne(sql, clazz);
    }

    /**
     * 调用存储过程
     *
     * @param sql sql
     */
    public static void callProcedure(String sql) {
        mapper.callProcedure(sql);
    }

    /**
     * 刷新物化视图
     *
     * @param mv             物化视图
     * @param isConcurrently 是否增量刷新 (是: 增量; 否: 全量)
     */
    public static void refreshMv(String mv, boolean isConcurrently) {
        mapper.refreshMv(mv, isConcurrently);
    }

}
